﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using HR60_PanelSoft.NetWork;     
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Service;
using HR60_PanelSoft.View.Utils;
using System.IO;

namespace HR60_PanelSoft.View
{
    //GC: [g1]编辑发布窗体[WindowPublic]
    //GC: [g2]调用通讯层接口[WindowPublic]
    //GC: [g3]设置发布网络功能[WindowPublic]
    //GC: [g4]联调[WindowPublic]
    //GC: [g5]联调1-8，在扫描网关前增加IP选择[WindowPublic]
    //GC: [g6]重构7-19-2,重构进度条bug[WindowPublic]
    //GC: [g7]联调7-24-2：新需求，网关设置更改到网关扫描位置[WindowPublic]
    //GC: [g8]bug2621、2706、2659[WindowPublic]
    
    


    /* [g6.s8] 取消继承IProcessHandle接口 */
    /* [g2.s2] 继承IProcessHandle接口 */
    public partial class WindowPublic : Form
   // public partial class WindowPublic : Form,IProcessHandle
    /* end of [g2.s2] */
    /* end of [g6.s8] */
    {
        #region   私有数据
        NetWork.NetService netS = new NetWork.NetService();

        /* [g2.s1] */
        NetWork.Dto.GateWayDto _curGW = null ; /* 当前网关 */
        Net _curNet = new Net();  /* 当前网络 */

        WindowSetDev _wsd = new WindowSetDev();
        /* end of [g2.s1] */

        /* [g2.s4]增加调用状态值 */
        bool _isSuccessful = true;
        /* end of [g2.s4] */

        /* [g6.s1] 将ComProgressBar下降至临时对象 */
        /* [g1.s1] */
        //ComProgressBar _cpb = null;
        /* end of [g1.s1] */
        /* end of [g6.s1] */

        #endregion

        /* [g1.s2] 记录上次发布状态 */
        /// <summary>
        /// 记录上次发布的信息
        /// </summary>
        private static string S_LastPublish = string.Empty;
        private bool _lastPublish_status = false;

        /* end of [g1.s2] */


        public WindowPublic()
		{
			InitializeComponent();

            //
            //panelNetGate.Controls.Add(LV_ONGW_GWLIST);
            //LV_ONGW_GWLIST.Dock = DockStyle.Bottom;
            //
            /* [g6.s2] */
            //_cpb = new ComProgressBar(this, " 正在通过网关发布... ");

            /* end of [g6.s2] */
            
			radioNetGate.Checked = true;

            /* [g1.s3] 重构控件 */
            //发布窗体，需要屏蔽捕获修改事件
            this.selNet.CatchValueChange = false;
            this.selMethod.CatchValueChange = false;
            this.CBOX_ONCOM_SELCOM.CatchValueChange = false;
            this.CBOX_ONCOM_SELNET.CatchValueChange = false;

            //添加网络下拉事件
            selNet.addDropDownEvent(onDropDownHandler_net);
            CBOX_ONCOM_SELNET.addDropDownEvent(onDropDownHandler_net);
            CBOX_ONCOM_SELCOM.addDropDownEvent(onDropDownHandler_com);


            //失能发布选择窗口，强制选择UDP方式
            selMethod.Enabled = false;
            List<Base.SelectItem> lsi = new List<Base.SelectItem>();
            lsi.Add(new Base.SelectItem("UDP", "UDP"));
            selMethod.cleanItems();
            selMethod.fillItems(lsi);

            /* end of [g1.s3] */
		}

		bool typeChangeFlag = false;

		private void radio_CheckedChanged(object sender, EventArgs e)
		{
			if (typeChangeFlag) return;
			typeChangeFlag = true;

			panelNetGate.Visible = radioNetGate.Checked;
			panelNetGateBtn.Visible = radioNetGate.Checked;
			panelCom.Visible = radioCom.Checked;
			panelComBtn.Visible = radioCom.Checked;

			typeChangeFlag = false;
		}

        /* [g1.s4] 填充网络下拉列表框 */
        private void fillNetCBox(Controls.TitleComboBox tcb)
        {
            List<HR60_PanelSoft.Model.Net> rst = Service.NetService.findNets();

            List<Base.SelectItem> src = new List<Base.SelectItem>();

            if (null == rst)
                return;
            foreach (HR60_PanelSoft.Model.Net n in rst)
            {
                Base.SelectItem s = new Base.SelectItem(n.name,(object)n);
                src.Add(s);
            }

            tcb.cleanItems();
            tcb.fillItems(src);
            
        }

        private void fillLVGateWay(List<NetWork.Dto.GateWayDto> lgw)
        {
            /* 无论如何，清理先 */
            LV_ONGW_GWLIST.Items.Clear();
            int idx = 1;
            if (null == lgw || lgw.Count < 1)
                return;
            foreach (NetWork.Dto.GateWayDto gwd in lgw)
            {
                ListViewItem lvi = new ListViewItem(idx++.ToString());
                /* [g8.s1] bug2659 */
                //lvi.SubItems.Add(gwd.NetNo);
                //lvi.SubItems.Add(gwd.DuanYuanNo);
                //lvi.SubItems.Add(gwd.MenPaiNo);
                
                lvi.SubItems.Add(gwd.DuanYuanNo);
                lvi.SubItems.Add(gwd.MenPaiNo);
                lvi.SubItems.Add(gwd.NetNo);
                /* end of [g8.s1] */
                lvi.SubItems.Add(gwd.PanelNo);
                lvi.SubItems.Add(gwd.SubNo);
                lvi.SubItems.Add(gwd.MAC);
                lvi.SubItems.Add(gwd.Ip);
                lvi.SubItems.Add(gwd.Ver);
                lvi.Tag = gwd;

                LV_ONGW_GWLIST.Items.Add(lvi);
            }

            /* 控制ListView size */
            /*
            if (lgw.Count > 4)
            {
                if (400 < LV_ONGW_GWLIST.Size.Height + 25 * (lgw.Count - 4))
                    return;
                LV_ONGW_GWLIST.Size = new Size(915, 108 + 25 * (lgw.Count - 4));
            }
            else
            {
                if (400 < LV_ONGW_GWLIST.Size.Height + 25 * (lgw.Count - 4))
                    LV_ONGW_GWLIST.Size = new Size(915, 108);
            }
             * */
        }



        /* [g6.s3] */

        //private void OnThreadProcess()
        //{
        //    _cpb.show();

        //    /* end of [g6.s3] */
        //}

        /* end of [g1.s4] */


        /// <summary>
        /// 填充串口下拉列表菜单
        /// </summary>
        /// <param name="tcb"></param>
        private void fillComCBox(Controls.TitleComboBox tcb)
        {
            string[] rst = System.IO.Ports.SerialPort.GetPortNames();
            List<Base.SelectItem> src = new List<Base.SelectItem>();
            foreach (string s in rst)
            {
                src.Add(new Base.SelectItem(s, s));
            }
            src.Sort();  /* 按名称进行排序，空字符最小 */
            tcb.cleanItems();
            tcb.fillItems(src);

        }



        //GC: get the ancestor typeof TitleComboBox,these codes should be reconstructed using reflection
        private Control getTitleCBAncestor(object son, object papa)
        {
            Control rst = (Control)son;
            if (rst.Parent == null)
                return null;   /* 返回null表示，没有TitleComboBox祖先 */
            rst = rst.Parent;
            View.Controls.TitleComboBox tcb = new Controls.TitleComboBox();
            if (rst.GetType() == tcb.GetType())
                return rst;
            else
                rst = getTitleCBAncestor(rst, rst.Parent);
            return rst;
        }

        #region 事件

        /* [g1.s5] 各类事件 */
        private void BTN_PUBLISH_GW_Click(object sender, EventArgs e)
        {
            /* [g3.s3]鲁棒 */
            if (null == _curGW)
            {
                View.ViewUtils.showAlert("请选择网关！");
                return;
            }
            /* end of [g3.s3] */

            /* [g7.s6]鲁棒 */
            _curNet = (Net)selNet.Value;
            if (_curNet == null)
            {
                View.ViewUtils.showAlert("请选择网络！");
                return;

            }
            /* end of [g7.s6] */
            /* [g6.s4] */
            //Thread thd = new System.Threading.Thread(new ThreadStart(OnThreadProcess));
            //thd.Start();
            ComProgressBar tempCpb = new ComProgressBar(this, "正在执行，请稍候...");
            tempCpb.Start();
            /* end of [g6.s4] */
            /* [g2.s5]取消demo，添加接口调用 */
            /* demo */
            //for (int i = 0; i < 11; i++)
            //{
            //    _cpb.Value = i;
            //    Thread.Sleep(1000);
            //}

            /* end of demo */
            try
            {
                netS.publishByGateWay(false, _curGW, _curNet, tempCpb);
                _lastPublish_status = true;

                ViewUtils.showMessage(" 通过网关发布成功! ");
            }
            catch (Exception ex)
            {
                View.ViewUtils.showError("通过网关发布失败：\r\n" + ex.Message);
                return;
            }
            finally
            {
                _lastPublish_status = false;

                if (tempCpb != null)
                    tempCpb.Stop();
            }
            //this.Close();
            ///* 更新发布状态 */
            //_lastPublish_status = true; //
            /* end of [g2.s5] */

            /* 记录上次发布的信息 */
            S_LastPublish = " 上次发布信息：网络号，" + "9" + "；IP，"+ "192.168.1.XXX " 
                + "；状态，" + (_lastPublish_status ? "成功" : "失败").ToString();
            if (_lastPublish_status)
                LBL_ONGW_LASTSTATUS.ForeColor = Color.Blue;
            else
                LBL_ONGW_LASTSTATUS.ForeColor = Color.Red;
            LBL_ONGW_LASTSTATUS.Text = S_LastPublish;
        }


        /* 串口发布按钮事件 */
        private void BTN_PUBLISH_COM_Click(object sender, EventArgs e)
        {

            _curNet = (Net)this.CBOX_ONCOM_SELNET.Value;
            /* [g7.s7]鲁棒 */
            if (_curNet == null)
            {
                View.ViewUtils.showAlert("请选择网络！");
                return;

            }
            /* end of [g7.s7] */
            /* [g6.s5] */
            //Thread thd = new System.Threading.Thread(new ThreadStart(OnThreadProcess));
            //thd.Start();
            ComProgressBar tempCpb = new ComProgressBar(this, "正在执行，请稍候...");
            tempCpb.Start();
            /* end of [g6.s5] */
            /* [g2.s6] 取消demo，增加通讯接口调用 */
            //for (int i = 1; i < 11; i++)
            //{
            //    //perNetGame.Value = i;
            //    _cpb.Value = i;
            //    System.Threading.Thread.Sleep(1000);
            //}
            
            //ViewUtils.showMessage(" Published by Com Successfully! ");
            //this.Close();

            string tempCom = this.CBOX_ONCOM_SELCOM.Value.ToString();
            if (tempCom == null)
            {
                //thd.Abort();
                tempCpb.Stop();
                View.ViewUtils.showAlert("串口不能为空！");
                return;
            }

            /* [g4.s8]修正_curNet为空bug */
            if (this.CBOX_ONCOM_SELNET.Value == null)
            {
                tempCpb.Stop();
                View.ViewUtils.showAlert("网络不能为空！");
                return;
            }

            /* end of [g4.s8] */
            try
            {

                //netS.publishByGateWay(false, _curGW, _curNet, this);
                netS.publishByCom(false, tempCom, _curNet, tempCpb);
                _lastPublish_status = true;

                ViewUtils.showMessage(" 通过串口发布成功! ");
            }
            catch (Exception ex)
            {
                View.ViewUtils.showError("通过串口发布失败：\r\n" + ex.Message);
                return;
            }
            finally
            {
                _lastPublish_status = false;

                if (tempCpb != null)
                    tempCpb.Stop();
            }
            
            //this.Close();

            /* end of [g2.s6] */
        }

        /* ComboBox下拉事件，用来填充网络列表 */
        private void onDropDownHandler_net(object sender, EventArgs e)
        {
            Control tcb = getTitleCBAncestor(sender, ((ComboBox)sender).Parent);
            if (tcb == null)
                return;
            
            fillNetCBox((Controls.TitleComboBox)tcb);
        }

        /*[g5.s2]增加Ip选择窗口*/

        private string GetIPByForm(List<string> IPs)
        {
            int tempBtnY,tempBtnX = 0;
            string rst = string.Empty;
            Form form = new Form();

            System.Windows.Forms.Panel pnl = new System.Windows.Forms.Panel();

            Button btnOK = new Button();
            Button btnCancel = new Button();
            ComboBox cbIPs = new ComboBox();
            Label lblNetCard = new Label();

            /* 设置窗口属性 */
            form.Text = "多网卡选择";
            form.StartPosition = FormStartPosition.CenterParent;
            form.Size = new Size(300, 150);
            form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            
            /* 设置控件属性 */

            tempBtnX = pnl.Size.Width - 30 - 60 - 30 - 60;
            tempBtnY = pnl.Size.Height - 20 - 30;

            pnl.Dock = DockStyle.Fill;

            btnOK.Size = btnCancel.Size = new Size(60, 30);
            btnCancel.Location = new Point(tempBtnX + 60 + 30, tempBtnY);
            btnOK.Location = new Point(tempBtnX, tempBtnY);
            btnOK.Text = "确定";
            btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            btnCancel.Text = "取消";
            btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;


            lblNetCard.AutoSize = false;
            lblNetCard.Size = new Size(50, 23);
            lblNetCard.Location = new Point(30,20);
            lblNetCard.Text = "网卡：";

            cbIPs.Size = new Size(180, 23);
            cbIPs.Location = new Point(30 + 50 + 10, 20);
            /*demo */
            //IPs.Add("192.168.55.11");
            //IPs.Add("192.168.55.12");
            //IPs.Add("192.168.55.13");
            /**/
            cbIPs.Items.Clear();
            foreach (string s in IPs)
            {
                cbIPs.Items.Add(s);
            }
            
            /* 设置按钮事件 */
            btnOK.Click += new EventHandler((object sender, EventArgs ea) => 
                {
                    if (cbIPs.SelectedItem == null)
                    {
                        rst = string.Empty;

                    }
                    else
                    {
                        rst = cbIPs.SelectedItem.ToString();
                    }



                });
            btnCancel.Click += new EventHandler((object sender, EventArgs ea) => 
                {
                    rst = string.Empty;
                });

            form.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs fcea) =>
                {
                    if (rst == string.Empty)
                    {
                        if (DialogResult.OK == MessageBox.Show("未选择网卡，无法进行网关扫描，是否重新选择网卡？",
                            "警告", MessageBoxButtons.OKCancel))
                        {
                            fcea.Cancel = true;
                        }
                        else
                        {
                            fcea.Cancel = false;
                        }
                    }
                });


            /* 添加控件 */
            pnl.Controls.Add(lblNetCard);
            pnl.Controls.Add(cbIPs);
            pnl.Controls.Add(btnOK);
            pnl.Controls.Add(btnCancel);

            form.Controls.Add(pnl);
            /* 显示设置窗口 */
            form.ShowDialog();     

            return rst;
        }

        /* end of [g5.s2] */
        //点击后搜索网关
        private void btnScan_Click(object sender, EventArgs e)
        {

            /* [g5.s1]多网卡选择 */
            List<string> tempIps = Tools.NetUtils.getIPAddress();
            if (tempIps.Count < 1)
            {
                View.ViewUtils.showError("未发现网卡，请检查当前计算机的网络设备！");
                return;
            }
            string tempSelectedIp = string.Empty;
            if (tempIps.Count > 1)
            {
                tempSelectedIp = GetIPByForm(tempIps);
                if (tempSelectedIp == string.Empty)
                {
                    View.ViewUtils.showError("选择的网卡配置不正确！");
                    return;
                }
            }
            else
                tempSelectedIp = tempIps[0];
            /* end of [g5.s1] */

            /* [g6.s6] */
            //Thread thd = new System.Threading.Thread(new ThreadStart(OnThreadProcess));
            //thd.Start();
            ComProgressBar tempCpb = new ComProgressBar(this, "正在执行，请稍候...");
            tempCpb.Start();
            /* end of [g6.s6] */

            List<NetWork.Dto.GateWayDto> lgw = new List<NetWork.Dto.GateWayDto>();

            /* [g4.s5] */
            try
            {
                /* [g4.s7] 扫描前，先清理 */
                if (this.LV_ONGW_GWLIST.Items.Count > 0) /* 清理列表记录 */
                {
                    this.LV_ONGW_GWLIST.Items.Clear();
                }

                this.txtIp.Value = string.Empty;

                this._curGW = null;  /* 清理当前网关对象 */

                /* end of [g4.s7] */
                lgw = netS.scanGateWay(tempSelectedIp, tempCpb);
                
            }
            catch (Exception ee)
            {
                //thd.Abort();
                View.ViewUtils.showError("扫描网关失败：\r\n" + ee.Message);
            }
            finally
            {
                tempCpb.Stop();
            }

            /* end of [g4.s5] */
            if (null == lgw || lgw.Count < 1)
            {
                //thd.Abort();
                tempCpb.Stop();
                ViewUtils.showMessage("未搜索到网关，请1分钟后再试！");
                return;
            }

            fillLVGateWay(lgw);
            /* [g5.s3]先填充listview再弹窗 */
            View.ViewUtils.showMessage("扫描网关成功！");
            /* end of [g5.s3] */
        }

        //ComboBox下拉事件，用来填充串口列表
        private void onDropDownHandler_com(object sender, EventArgs e)
        {
            Control tcb = getTitleCBAncestor(sender, ((ComboBox)sender).Parent);
            if (tcb == null)
                return;
            fillComCBox((Controls.TitleComboBox)tcb);
        }

        //网关列表更改选项事件
        private void LV_ONGW_GWLIST_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LV_ONGW_GWLIST.SelectedItems.Count < 1)
                return;
            /* [g3.s2] 清理内存对象，保持实时状态 */
            _curGW = null;
            /* end of [g3.s2] */
            bool isInNets = false;
            ListViewItem lvi = LV_ONGW_GWLIST.SelectedItems[0];
            NetWork.Dto.GateWayDto gwd = (NetWork.Dto.GateWayDto)lvi.Tag;
            int netid = -1;
            if (!int.TryParse(gwd.NetNo, out netid))
            {
                ViewUtils.showError("网络号错误，请重新搜索网关！");
                return;
            }

            

            foreach (Model.Net n in Proj.currentProj.nets)
            {
                if (netid == n.index)
                {
                    isInNets = true;
                    if (!selNet.isFilledItem)
                        fillNetCBox(selNet);
                    selNet.Value = n;
                    txtIp.Value = gwd.Ip;  /* 获取ip */
                    _curNet = n; /* 获取内存对象 */
                }
                else
                    continue;
            }

            /* [g7.s3]取消该逻辑 */
            //if (!isInNets)
            //{
            //    ViewUtils.showAlert("工程中没有此网络，请重新确认！");
            //    return;
            //}
            /* end of [g7.s3] */
            /* [g3.s1] 选中网关，保存到内存对象 */
            _curGW = gwd;
            /* end of [g3.s1] */

        }

        private void BTN_ONGW_GENCONF_Click(object sender, EventArgs e)
        {

            _curNet = (Net)selNet.Value;
            if (_curNet == null)
            {
                View.ViewUtils.showAlert("请选择网络！");
                return;

            }

            if (_curGW == null)
            {
                View.ViewUtils.showAlert("请选择网关！");
                return;
            }

            /* 1. 打开指定路径 */
            //System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
            //if (DialogResult.OK == ofd.ShowDialog())
            //{
            //    if (ofd.CheckPathExists)
            //        tempPath = ofd.FileName;
            //    else
            //        tempPath = string.Empty;
            //}
            //if (tempPath == string.Empty)
            //{
            //    View.ViewUtils.showAlert("文件不存在！");
            //    return;
            //}

            /* 2. 调用生成设置文件接口 */
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            /* [g4.s1] 增加异常捕获 */
            try
            {
                netS.createSettingFile(false, _curGW, _curNet, ref ms);
            }
            catch(Exception ee)
            {
                View.ViewUtils.showError("生成配置文件流错误：\r\n" + ee.Message);
                return;
            }

            /* end of [g4.s1] */
            System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();

            /* [g5.s4]增加过滤器 */
            /*
             * 默认HK60_net.txt
             */
            sfd.Filter = "文本文件|*.txt";
            sfd.FileName = "HK60_net.txt";
            /* end of [g5.s4] */
            if (DialogResult.OK == sfd.ShowDialog())
            {

                /* [g4.s6] 调用生产配置文件接口 */
                //System.IO.File.Create(sfd.FileName);
                try
                {
                    //System.IO.File.Create(sfd.FileName);
                    //File.WriteAllBytes(sfd.FileName, ms.GetBuffer());
                    //ms.Close();

                    System.IO.FileStream fs = new FileStream(sfd.FileName,FileMode.OpenOrCreate,FileAccess.Write);
                    ms.WriteTo(fs);
                    ms.Close();
                    ms.Dispose();

                    fs.Close();
                    fs.Dispose();

                    /* [g4.s3]发送成功提示 */
                    View.ViewUtils.showMessage("生成配置文件成功！");
                    /* end of [g4.s3] */

                }
                catch (Exception ee)
                {
                    View.ViewUtils.showError("生成配置文件错误：\r\n" + ee.Message);
                    return;
                }
                finally
                {
                    ms.Close();
                    ms.Dispose();


                }

                /* end of [g4.s6] */
            }
            else
            {
                View.ViewUtils.showMessage("生成配置文件失败！");
            }

        }

        private void BTN_ONGW_SENDCONF_Click(object sender, EventArgs e)
        {
            //if (_curNet == null)
            //{
            //    View.ViewUtils.showAlert("请选择网络！");
            //    return;

            //}

            if (_curGW == null)
            {
                View.ViewUtils.showAlert("请选择网关！");
                return;
            }

            string tempPath = string.Empty;
            System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
            if (DialogResult.OK == ofd.ShowDialog())
            {
                if (ofd.CheckPathExists)
                    tempPath = ofd.FileName;
                else
                    tempPath = string.Empty;
            }
            if (tempPath == string.Empty)
            {
                View.ViewUtils.showAlert("文件不存在！");
                return;
            }

            /* [g4.s2]捕获异常 */
            try
            {
                netS.publishSettingFile(_curGW, tempPath);
            }
            catch (Exception ee)
            {
                View.ViewUtils.showError("发送配置文件错误：\r\n" + ee.Message);
                return;
            }
            View.ViewUtils.showMessage("发送配置文件成功！");


        }

        /* [g1.s6] 增加更新网关按钮 */
        private void BTN_ONGW_UPDATE_Click(object sender, EventArgs e)
        {
            /* [g3.s4] 鲁棒 */
            if (null == _curGW)
            {
                View.ViewUtils.showAlert("请选择网关！");
                return;
            }
            /* end of [g3.s4] */
            /* 1. 打开指定路径 */
            string tempPath = string.Empty;
            System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
            if (DialogResult.OK == ofd.ShowDialog())
            {
                if (ofd.CheckPathExists)
                    tempPath = ofd.FileName;
                else
                    tempPath = string.Empty;
            }
            if (tempPath == string.Empty)
            {
                View.ViewUtils.showAlert("文件不存在！");
                return;
            }

            /* 2. 调用进度条 */
            /* [g6.s7] */
            //Thread thd = new System.Threading.Thread(new ThreadStart(OnThreadProcess));
            //thd.Start();
            
            //_cpb.Value = 0;
            ComProgressBar tempCpb = new ComProgressBar(this, "正在执行，请稍候...");
            tempCpb.Start();

            /* end of [g6.s7] */
            /* 3. 获得全路径，升级网关 */
            try
            {
                netS.updateGateWay(_curGW, tempPath, tempCpb);
                /* [g4.s4] 增加更新网关提示 */
                View.ViewUtils.showMessage("更新网关成功！");
                /* end of [g4.s4] */

            }
            catch (Exception ee)
            {
                View.ViewUtils.showError("更新网关失败：\r\n" + ee.Message);
           }
            finally
            {
                //thd.Abort();
                tempCpb.Start();
            }



        }

        private void BTN_ONPUBLIC_SETDEV_Click(object sender, EventArgs e)
        {
            if (null == _curGW)
            {
                View.ViewUtils.showAlert("请选择网关！");
                return;
            }
            int tempUnitId = -1, tempRoomId = -1, tempNetId = -1, tempPnlID = -1;

            if (!(int.TryParse(_curGW.DuanYuanNo, out tempUnitId) && int.TryParse(_curGW.MenPaiNo, out tempRoomId)
                && int.TryParse(_curGW.NetNo, out tempNetId) && int.TryParse(_curGW.PanelNo, out tempPnlID)))
            {
                View.ViewUtils.showAlert("网关参数不正确，请重新选择网关！");
                return;
            }

            _wsd.SetProperties(tempUnitId, tempRoomId, tempNetId, tempPnlID, _curGW);
            _wsd.ShowDialog();

        }
        /* [g7.s1]双击操作，弹出设置窗口 */
        private void LV_ONGW_GWLIST_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            /* 选中所选 */
            ListViewItem tempLvi = new ListViewItem();
            tempLvi = LV_ONGW_GWLIST.GetItemAt(e.X, e.Y);
            if (tempLvi == null)
                return;
            tempLvi.Selected = true; 

            /* 配置网关 */
            _curGW = null;
            _curGW = (NetWork.Dto.GateWayDto)tempLvi.Tag;
            if (_curGW == null)
            {
                //GCERR:
                return;
            }
            /* [g8.s2]选中后填充到IP文本框 */
            this.txtIp.Value = _curGW.Ip;
            /* end of [g8.s2] */
           
            /* 弹出设置窗口 */
            WindowSetting tempWs = new WindowSetting(_curGW);
            if (DialogResult.OK == tempWs.ShowDialog() && tempWs.IsSuccessed)
            {
                /* [g7.s4]反射到选中节点 */
                _curGW = tempWs.GWD;
                
                /* 刷新ListViewItem */
                /* [g8.s3] */
                //tempLvi.SubItems[1].Text = _curGW.NetNo;
                //tempLvi.SubItems[2].Text = _curGW.MenPaiNo;
                //tempLvi.SubItems[3].Text = _curGW.DuanYuanNo;
                tempLvi.SubItems[1].Text = _curGW.DuanYuanNo;
                tempLvi.SubItems[2].Text = _curGW.MenPaiNo;
                tempLvi.SubItems[3].Text = _curGW.NetNo;
                /* end of [g8.s3] */
                tempLvi.SubItems[4].Text = _curGW.PanelNo;
                tempLvi.SubItems[5].Text = _curGW.SubNo;

                this.LV_ONGW_GWLIST.Refresh();
                /*  */
                View.ViewUtils.showMessage("网关设置成功！");
            }

        }


        
        /* end of [g7.s1] */


        /* end of [g1.s6] */

        /* end of [g1.s5] */
        #endregion

        /* [g6.s4] */

        /* [g2.s3] 填充IProcessHandle接口 */

        //public void onError(string errStr)
        //{
        //    _isSuccessful = false;
        //    _cpb.Value = ComProgressBar.MaxValue;
        //    View.ViewUtils.showAlert(errStr);
        //}

        //public void onProcess(int percent)
        //{
        //    _cpb.Value = percent;
        //}



        /* end of [g2.s3] */

        /* end of [g6.s4] */




    }
}
